/// 群组互动墙相关数据模型

/// 互动分享项
class GroupInteraction {
  final int id;
  final int groupId;
  final int userId;
  final String dailyValue;
  final String? lingTiao;
  final String? sunPillar;
  final String? healthReviews;
  final String? healthAdvice;
  final String? healthTraceability;
  final String? funEasterEggs;
  final bool isAutomatic;
  final String? content;
  final String shareMemberName;
  final String shareMemberAvatar;
  final List<ShareComment> comments;
  final List<ShareThumb> thumbs;

  GroupInteraction({
    required this.id,
    required this.groupId,
    required this.userId,
    required this.dailyValue,
    this.lingTiao,
    this.sunPillar,
    this.healthReviews,
    this.healthAdvice,
    this.healthTraceability,
    this.funEasterEggs,
    required this.isAutomatic,
    this.content,
    required this.shareMemberName,
    required this.shareMemberAvatar,
    required this.comments,
    required this.thumbs,
  });

  factory GroupInteraction.fromJson(Map<String, dynamic> json) {
    final commentList = json['shareCommentList'] as List? ?? [];
    final thumbList = json['shareThumbList'] as List? ?? [];

    return GroupInteraction(
      id: json['id'] as int,
      groupId: json['groupId'] as int,
      userId: json['userId'] as int,
      dailyValue: json['dailyValue'] as String? ?? '',
      lingTiao: json['lingTiao'] as String?,
      sunPillar: json['sunPillar'] as String?,
      healthReviews: json['healthReviews'] as String?,
      healthAdvice: json['healthAdvice'] as String?,
      healthTraceability: json['healthTraceability'] as String?,
      funEasterEggs: json['funEasterEggs'] as String?,
      isAutomatic: json['isAutomatic']?.toString() == '1',
      content: json['content'] as String?,
      shareMemberName: json['shareMemberName'] as String? ?? '',
      shareMemberAvatar: json['shareMemberAvatar'] as String? ?? '',
      comments: commentList.map((e) => ShareComment.fromJson(e)).toList(),
      thumbs: thumbList.map((e) => ShareThumb.fromJson(e)).toList(),
    );
  }

  /// 获取点赞数
  int get likesCount => thumbs.length;

  /// 获取评论数
  int get commentsCount => comments.length;

  /// 检查用户是否已点赞
  bool isLikedBy(int userId) {
    return thumbs.any((thumb) => thumb.thumbUserId == userId);
  }

  /// 获取用户的点赞记录
  ShareThumb? getThumbBy(int userId) {
    try {
      return thumbs.firstWhere((thumb) => thumb.thumbUserId == userId);
    } catch (e) {
      return null;
    }
  }

  /// 获取点赞头像列表
  List<String> getLikedAvatars(String baseUrl) {
    return thumbs
        .map((thumb) => _getFullAvatarUrl(thumb.thumbMemberAvatar, baseUrl))
        .toList();
  }

  String _getFullAvatarUrl(String? avatar, String baseUrl) {
    if (avatar == null || avatar.isEmpty) return '';
    if (avatar.startsWith('http://') || avatar.startsWith('https://')) {
      return avatar;
    }
    return '$baseUrl$avatar';
  }
}

/// 分享评论
class ShareComment {
  final int commentId;
  final int commentUserId;
  final String commentContent;
  final String commentMemberName;
  final String commentMemberAvatar;

  ShareComment({
    required this.commentId,
    required this.commentUserId,
    required this.commentContent,
    required this.commentMemberName,
    required this.commentMemberAvatar,
  });

  factory ShareComment.fromJson(Map<String, dynamic> json) {
    return ShareComment(
      commentId: json['commentId'] as int,
      commentUserId: _parseUserId(json['commentUserId']),
      commentContent: json['commentContent'] as String? ?? '',
      commentMemberName: json['commentMemberName'] as String? ?? '',
      commentMemberAvatar: json['commentMemberAvatar'] as String? ?? '',
    );
  }

  static int _parseUserId(dynamic userId) {
    if (userId is int) return userId;
    if (userId is String) return int.tryParse(userId) ?? 0;
    return 0;
  }
}

/// 分享点赞
class ShareThumb {
  final int thumbId;
  final int thumbUserId;
  final String thumbMemberName;
  final String thumbMemberAvatar;

  ShareThumb({
    required this.thumbId,
    required this.thumbUserId,
    required this.thumbMemberName,
    required this.thumbMemberAvatar,
  });

  factory ShareThumb.fromJson(Map<String, dynamic> json) {
    return ShareThumb(
      thumbId: json['thumbId'] as int,
      thumbUserId: _parseUserId(json['thumbUserId']),
      thumbMemberName: json['thumbMemberName'] as String? ?? '',
      thumbMemberAvatar: json['thumbMemberAvatar'] as String? ?? '',
    );
  }

  static int _parseUserId(dynamic userId) {
    if (userId is int) return userId;
    if (userId is String) return int.tryParse(userId) ?? 0;
    return 0;
  }
}

/// 群组成员数据
class GroupMemberData {
  final int userId;
  final String userName;
  final int? value;
  final bool isQualify;
  final bool isOptimum;

  GroupMemberData({
    required this.userId,
    required this.userName,
    this.value,
    required this.isQualify,
    required this.isOptimum,
  });

  factory GroupMemberData.fromJson(Map<String, dynamic> json) {
    return GroupMemberData(
      userId: json['userId'] as int,
      userName: json['userName'] as String? ?? '',
      value: json['value'] as int?,
      isQualify: json['isQualify']?.toString() == '1',
      isOptimum: json['optimum']?.toString() == '1',
    );
  }

  void operator [](String other) {}
}
